package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.dto.LeaderDto;
import com.ruoyi.common.core.cg.pojo.Community;
import com.ruoyi.common.core.cg.pojo.Group;
import com.ruoyi.common.core.cg.pojo.Leader;
import com.ruoyi.common.core.cg.pojo.Store;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.manage.mapper.CommunityMapper;
import com.ruoyi.manage.mapper.GroupMapper;
import com.ruoyi.manage.mapper.LeaderMapper;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.service.LeaderService;
import io.lettuce.core.ScriptOutputType;
import org.bouncycastle.util.Pack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 马景昌
 * @create 2025/03/11
 * @description
 */
@Service
public class LeaderServiceImpl implements LeaderService {
    @Autowired
    private LeaderMapper leaderMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Result pageShow(Leader leader) {
        PageHelper.startPage(leader.getPageNum(),leader.getPageSize());
        List<Leader> leaders = leaderMapper.pageShow(leader);
        System.err.println(leaders);
        for (Leader leader1 : leaders) {
//           查询拼团点数量
            Integer integer = leaderMapper.selectGroupCount(leader1.getId());
            leader1.setGroupCount(integer);
//            查询店铺数量
            Integer integer1 = leaderMapper.selectStoreCount(leader1.getId());
            leader1.setStoreCount(integer1);
//            查询社区地点名称
            Community community = communityMapper.selectCommunityName(leader1.getId());
            if(community==null){
                leader1.setCommunityName("暂无");
            }else {
                leader1.setCommunityName(community.getTitle());
            }
//            一个leader可以有多个拼团  查询金额
            List<Group> getprice = groupMapper.getprice(leader1.getId());
            for (Group group : getprice) {
                BigDecimal getprice1 = orderMapper.getprice(group.getId());
                if(getprice1 != null){
                    // 获取当前的money值
                    BigDecimal currentMoney = leader1.getMoney() != null ? leader1.getMoney() : BigDecimal.ZERO;
                    // 将getprice1加到currentMoney上
                    BigDecimal totalMoney = currentMoney.add(getprice1);
                    // 设置增加后的总金额回到leader1
                    leader1.setMoney(totalMoney);
                }
            }
        }
        PageInfo<Leader> pageInfo = new PageInfo<>(leaders);
        System.err.println(pageInfo.getTotal());
        return new Result(true,"查询成功",pageInfo);
    }

    @Override
    public Result info(Integer id) {
        Leader leader = leaderMapper.info(id);
//        查询拼团点名称
        Community community = communityMapper.selectCommunityName(leader.getId());
        leader.setCommunityName(community.getTitle());
        return new Result(true,"查询成功",leader);
    }

    @Override
    public Result put(LeaderDto leader) {
        boolean b = leaderMapper.updateLeader(leader);
        if(!b){
            return new Result(false,"修改失败");
        }else {
//            社区表根据leaderid查询
            Community community = communityMapper.selectByLeaderId(leader.getId());
//            查询出来做修改社区名称
            communityMapper.updataByLeaderIdTitle(community.getId(),leader.getCommunityName());
            return new Result(true,"修改成功");
        }
    }

    @Override
    public Result show() {
        List<Leader> show = leaderMapper.show();
        return new Result(true,"查询成功",show);
    }

    @Override
    public Result showPhone() {
        List<Leader> leaders = leaderMapper.showPhone();
        return new Result(true,"查询成功",leaders);
    }

    @Override
    public Result showIdCard() {
        List<Leader> leaders = leaderMapper.showIdCard();
        return new Result(true,"查询成功",leaders);
    }
}
